package com.uchedao.vehicledetection.view.carlist;


import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.uchedao.mybaselibrary.activity.BaseActivity;
import com.uchedao.mybaselibrary.utils.LogUtil;
import com.uchedao.vehicledetection.R;
import com.uchedao.vehicledetection.view.baseinfo.BaseInfoCarInfoView;
import com.uchedao.vehicledetection.view.carlist.Entity.CarEntity;
import com.uchedao.vehicledetection.view.carlist.fragment.FragmentBrand;
import com.uchedao.vehicledetection.view.carlist.fragment.FragmentModel;
import com.uchedao.vehicledetection.view.carlist.fragment.FragmentSeries;

/**
 * Created by ZhangZhanghua on 2015/9/14 0014.
 */
public class CarActivity extends BaseActivity implements FragmentBrand.IBrandClickListener , FragmentSeries.ISeriesClickListener , FragmentModel.IModelClickListener{
    private FragmentBrand fBrand;
    private FragmentModel fModel;
    private FragmentSeries fSeries;
    private int bId ,sId , mId;
    private String bName , sName , mName;
    private TextView tvBrand, tvSeries, tv1;
    private FragmentManager fm;
    FragmentTransaction ft;
    private LinearLayout ll;
    private Intent intent;
    private CarEntity carEntity;
    private Bundle bundle;

    @Override
    protected String getActivityTag() {
        return "CarActivity";
    }

    @Override
    protected int getLayoutResID() {
        return R.layout.activity_car;
    }

    @Override
    protected void initView() {
        TextView tv = (TextView) findViewById(R.id.tv_title_name);
        tv.setText("车辆选择");
        carEntity = new CarEntity();
        ll = (LinearLayout) findViewById(R.id.ll_container);
        tvBrand = (TextView) findViewById(R.id.tv_brand);
        tvSeries = (TextView) findViewById(R.id.tv_series);
        tv1 = (TextView) findViewById(R.id.tv_1);
        ll.setVisibility(View.GONE);
        setTvGone();
        fm = getFragmentManager();
        ft = fm.beginTransaction();
        if(getIntent().getExtras() != null){
           switch (getIntent().getExtras().getInt("carSelect")){
               case 1:
                   fSeries = new FragmentSeries();
                   ft.add(R.id.content,fSeries);
                   ft.commit();
                   break;
               case 2:
                   fModel = new FragmentModel();
                   ft.add(R.id.content,fModel);
                   ft.commit();
                   break;
           }

        }else {
            fBrand = new FragmentBrand();
            ft.add(R.id.content,fBrand);
            ft.commit();
        }

    }

    @Override
    protected void initData() {

    }

    @Override
    protected void initListener() {

        findViewById(R.id.title_back).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
//        if (tvBrand.getVisibility() == View.VISIBLE){
            tvBrand.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    ft = fm.beginTransaction();
                    ft.replace(R.id.content, fBrand);
                    ft.commit();
                    setTvGone();
                    ll.setVisibility(View.GONE);
                }
            });
//        }
//        if (tvSeries.getVisibility() == View.VISIBLE){
        tvSeries.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Bundle args = new Bundle();
                args.putInt("carBrandId", bId);
                fSeries.setArguments(args);
                ft = fm.beginTransaction();
                ft.replace(R.id.content, fSeries);
                setTvGone();
                setTvVisible(tvBrand);
                setTvVisible(tv1);
                    ft.commit();
                }
            });
//        }
    }

    @Override
    public void onBrandClick(int bId, String bName) {
        ll.setVisibility(View.VISIBLE);
        this.bId = bId;
        this.bName = bName;
        Bundle args = new Bundle();
        args.putInt("carBrandId", this.bId);
        setTvGone();
        setTvVisible(tvBrand);
        tvBrand.setText(bName);
        if (fSeries == null){
            fSeries = new FragmentSeries();
        }
        fSeries.setArguments(args);
        ft = fm.beginTransaction();
//        ft.hide(fBrand);
//        ft.add(R.id.content, fSeries);
        ft.replace(R.id.content, fSeries);
//        ft.addToBackStack(null);
        ft.commit();
//        carEntity = new CarEntity();
        carEntity.setbId(this.bId);
        carEntity.setbName(this.bName);
        intent = new Intent();
        bundle = new Bundle();
        bundle.putSerializable("carEntity",carEntity);
        intent.putExtras(bundle);
        setResult(BaseInfoCarInfoView.REQUEST_CAR_BRAND, intent);
    }

    @Override
    public void onSeriesClick(int sId, String sName) {
        this.sId = sId;
        this.sName = sName;
        Bundle args = new Bundle();
        args.putInt("carSeriesId", this.sId);
        setTvGone();
        setTvVisible(tvBrand);
        tvBrand.setText(bName);
        setTvVisible(tv1);
        setTvVisible(tvSeries);
        tvSeries.setText(sName);
        if (fModel == null){
            fModel = new FragmentModel();
        }
        fModel.setArguments(args);
        ft = fm.beginTransaction();
        ft.replace(R.id.content, fModel);
//        ft.addToBackStack(null);
        ft.commit();
//        carEntity = new CarEntity();
        carEntity.setsId(this.sId);
        carEntity.setsName(this.sName);
        intent = new Intent();
        bundle = new Bundle();
        bundle.putSerializable("carEntity", carEntity);
        intent.putExtras(bundle);
        setResult(BaseInfoCarInfoView.REQUEST_CAR_SERIES, intent);
    }
    public void setTvGone(){
       tvBrand.setVisibility(View.GONE);
       tvSeries.setVisibility(View.GONE);
       tv1.setVisibility(View.GONE);
    }
    public void setTvVisible(TextView tv){
        tv.setVisibility(View.VISIBLE);
    }

    @Override
    public void onModelClick(int mId, String mName) {
        if (mId != 0){
            this.mId = mId;
        }
        if (!mName.equals("0")){
           this.mName = mName;
        }else {
           this.mName = "";
        }
//        carEntity = new CarEntity();
        carEntity.setmId(this.mId);
        carEntity.setmName(this.mName);
        intent = new Intent();
        bundle = new Bundle();
        bundle.putSerializable(BaseInfoCarInfoView.KEY_DATA, carEntity);
        intent.putExtras(bundle);
        setResult(RESULT_OK, intent);
        LogUtil.e("bId:"+bId+"sId:"+sId+"mId:"+this.mId+"bName:"+bName+"sName:"+sName+"mName:"+this.mName);
    }

//    public class CarEntity implements Serializable{
//        private int bId;
//        private int sId;
//        private int mId;
//        private String bName;
//        private String sName;
//        private String mName;
//
//        public int getbId() {
//            return bId;
//        }
//
//        public void setbId(int bId) {
//            this.bId = bId;
//        }
//
//        public int getsId() {
//            return sId;
//        }
//
//        public void setsId(int sId) {
//            this.sId = sId;
//        }
//
//        public int getmId() {
//            return mId;
//        }
//
//        public void setmId(int mId) {
//            this.mId = mId;
//        }
//
//        public String getbName() {
//            return bName;
//        }
//
//        public void setbName(String bName) {
//            this.bName = bName;
//        }
//
//        public String getsName() {
//            return sName;
//        }
//
//        public void setsName(String sName) {
//            this.sName = sName;
//        }
//
//        public String getmName() {
//            return mName;
//        }
//
//        public void setmName(String mName) {
//            this.mName = mName;
//        }
//    }
}
